package cn.gcjxsc.service.integral;

import cn.gcjxsc.data.vo.salesman.AccountVo;
import cn.gcjxsc.entity.salesman.Salesman;
import cn.gcjxsc.entity.salesman.SalesmanAccount;
import cn.gcjxsc.repository.salesaman.SalesmanAccountRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author pumbaa
 * @since 1.0
 */
@Service
public class SalesmanAccountService {
    @Autowired
    private SalesmanAccountRepository accountRepository;

    public List<SalesmanAccount> findByMemberId(Integer memberId){
        return accountRepository.findByMemberId(memberId);
    }

    public SalesmanAccount findBySalesman(Salesman salesman) {
        SalesmanAccount account = accountRepository.findBySalesmanId(salesman.getId());
        if (null == account) {
            account = new SalesmanAccount();
            account.setSalesmanId(salesman.getId());
            account.setMemberId(salesman.getMemberId());
        }
        return account;
    }

    @Transactional
    public SalesmanAccount saveAccount(SalesmanAccount account){
        return accountRepository.save(account);
    }

    public AccountVo convert(SalesmanAccount account) {
        AccountVo vo = new AccountVo();
        BeanUtils.copyProperties(account, vo);
        return vo;
    }
}
